import { reqRegister } from "../../request/api"
import { Toast } from "antd-mobile"
//初始状态
const initState = {
    user: {
        phone: '',
        nickname: '',
        password: ''
    }
}

// actionType
const TYPES = {
    REGISTER_CHANGE_USER: 'REGISTER_CHANGE_USER'
}

// reducer
const reducer = (state = initState, action) => {
    switch (action.type) {
        case TYPES.REGISTER_CHANGE_USER:
            return {
                ...state,
                user: {
                    ...state.user,
                    [action.key]: action.value
                }
            }
        default:
            return state;
    }
}

// actionCreators
export const actions = {
    // 修改user
    changeUser: (key, value) => ({ type: TYPES.REGISTER_CHANGE_USER, key, value }),
    // 点击注册按钮
    doRegister: (push) => {
        return (dispatch, getState) => {
            reqRegister(getUser(getState())).then(res => {
                if (res.data.code == 200) {
                    Toast.show({
                        content: res.data.msg
                    })
                    // 跳转至登录
                    push('/login')
                }
            })
        }
    }

}
// selectors
export const getUser = state => state.register.user;

export default reducer